<template>
  <div class="mod-sp__spmatchitem">
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-button @click="getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('sp:spmatchitem:upload')" @click="uploadPormotion()" type="primary">上传晋级</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('sp:spmatchitem:upload')" @click="downSign()" type="primary">导出报名</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="exportHandleExa()">{{ $t("export") }}</el-button>
      </el-form-item>
      <!--
      <el-form-item>
        <el-button type="primary" >赛项管理</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('sp:spmatchitem:save')" type="primary" @click="addOrUpdateHandleX()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('sp:spmatchitem:delete')" type="danger" @click="deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
      -->
    </el-form>

    <el-table ref="multipleTable" v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <!--
      <el-table-column prop="matchName" label="赛事名称" header-align="center" align="center"></el-table-column>
      -->
      <el-table-column prop="baseMatchItemName" label="赛项名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="total.groupsNum" label="报名队伍数" header-align="center" align="center"></el-table-column>
      <el-table-column prop="total.studentNum" label="报名人数" header-align="center" align="center"></el-table-column>
      <el-table-column prop="total.teacherNum" label="指导老师数" header-align="center" align="center"></el-table-column>
      <el-table-column prop="total.teamsNum" label="代表队数" header-align="center" align="center"></el-table-column>

      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button type="text" size="small" @click="toSpMatchItem(scope.row.id, scope.row.itemType)">赛项管理</el-button>
          <el-button type="text" size="small" @click="noticeItemHandle(scope.row.id)">发布通知</el-button>
          <el-button type="text" size="small" @click="exportHandleSign(scope.row.id, scope.row.itemType)">参赛表单</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!--上传晋级-->
    <upload v-if="uploadVisible2" ref="upload2" uploadurl="/sp/spmatchitemteam/importPormotion" downloadurl="/sp/spmatchitemteam/exportError1" :matchId="dataForm.matchId" @refreshDataList="getDataList"></upload>
    <!--发布通知-->
    <notice-item v-if="noticeItemVisible" ref="noticeItem"></notice-item>
  </div>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import AddOrUpdate from "./spmatchitem-add-or-update.vue";
import Upload from "./score-upload.vue";
import baseService from "@/service/baseService";
import noticeItem from "./spnoticeitem-add-or-update.vue";
import app from "@/constants/app";
import qs from "qs";
import { getToken } from "@/utils/cache";
export default defineComponent({
  components: {
    AddOrUpdate,
    Upload,
    noticeItem
  },
  props: { dictTypeId: String, title: String },
  setup() {
    const state = reactive({
      uploadVisible2: false,
      noticeItemVisible: false,
      getDataListURL: "/sp/spmatchitem/page",
      getDataListIsPage: true,
      exportURL: "/sp/spmatchitem/export",
      deleteURL: "/sp/spmatchitem",
      deleteIsBatch: true,
      dataForm: {
        matchId: "0",
        type: "pass"
      },
      formTitle: ""
    });
    return { ...useView(state), ...toRefs(state) };
  },
  created() {
    this.dataForm.matchId = this.dictTypeId as string;
    this.formTitle = this.title as string;
    this.getDataList();
  },
  watch: {
    dictTypeId() {
      this.dataForm.matchId = this.dictTypeId as string;
      this.getDataList();
    }
  },
  methods: {
    // 新增 / 修改
    addOrUpdateHandleX(id?: string) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.dataForm.id = id;
        this.$refs.addOrUpdate.dataForm.matchId = this.dataForm.matchId;
        this.$refs.addOrUpdate.init();
      });
    },
    // 跳转到赛项管理
    toSpMatchItem(id?: string, itemType?: string) {
      this.$router.push({ path: "/sp/spmatchitemdetail", query: { id: id, itemType: itemType } });
    },

    uploadPormotion() {
      this.uploadVisible2 = true;
      this.$nextTick(() => {
        this.$refs.upload2.init();
      });
    },
    noticeItemHandle(id?: string) {
      this.noticeItemVisible = true;
      this.$nextTick(() => {
        this.$refs.noticeItem.id = id;
        this.$refs.noticeItem.init();
      });
    },
    // 导出
    exportHandleExa(id?: string) {
      window.open(
        `${app.api}/sp/spmatchitem/exportCount?${qs.stringify({
          ...this.dataForm,
          token: getToken()
        })}`
      );
    },
    downSign() {
      const items = [];
      this.dataListSelections.forEach((item: any) => {
        items.push(item.id);
      });
      let itemQ = "";
      if (items.length > 0) {
        itemQ = "items=" + items.join(",") + "&";
      }
      window.open(
        `${app.api}/sp/spmatchReport/exportSignWord?${itemQ}${qs.stringify({
          ...this.dataForm,
          token: getToken()
        })}`
      );
    },

    //参赛表单导出
    exportHandleSign(id?: string, itemType?: string) {
      let method = 'exportStudent';
      //个人
      if (itemType == '100201') {
        method = 'exportStudent';
      } else {
        method = 'signRecordExport';
      }
      window.open(
        `${app.api}/sp/spmatchitemteam/` + method+ `?${qs.stringify({
          matchItemId: id,
          token: getToken()
        })}`
      );
    },

  }
});
</script>
